package com.pucong.gitee.database.backup.business.service.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pucong.gitee.database.backup.business.service.mapper.ServiceLinuxLogMapper;
import com.pucong.gitee.database.backup.business.sys.service.SysUserInfoService;
import com.pucong.gitee.database.backup.common.entity.service.ServiceLinuxLog;
import com.pucong.gitee.database.backup.common.entity.service.dto.ServiceLinuxLogSaveDto;
import com.pucong.gitee.database.backup.common.entity.service.dto.ServiceLinuxLogSelectDto;
import com.pucong.gitee.database.backup.utils.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ServiceLinuxLogServiceImpl extends ServiceImpl<ServiceLinuxLogMapper,ServiceLinuxLog> implements ServiceLinuxLogService  {

    @Autowired
    private ServiceLinuxLogMapper serviceLinuxLogMapper;

    @Autowired
    private SysUserInfoService sysUserInfoService;

    @Override
    public IPage<ServiceLinuxLog> getLinuxLogListByPage(int pageNum, int pageSize, ServiceLinuxLogSelectDto dto) {
        // 查询用户的执行记录
        List<String> ids = sysUserInfoService.selectChildrenUserId();

        return null;
    }

    @Override
    public ServiceLinuxLog getLinuxLogById(String id) {
        return null;
    }

    @Override
    public void addLog(ServiceLinuxLogSaveDto dto) {
        // 执行记录
        ServiceLinuxLog insertLog = new ServiceLinuxLog();
        insertLog.setUuid(UUID.uuid());
        insertLog.setSessionId(dto.getSessionId());
        insertLog.setServiceId(dto.getServiceId());
        insertLog.setExeTime(new Date());
        insertLog.setExeBy(dto.getUserId());
        insertLog.setExeCmd(dto.getExeCmd());
        serviceLinuxLogMapper.insert(insertLog);
    }

    @Override
    public void addBatchLog(List<ServiceLinuxLogSaveDto> dtoList) {
        List<ServiceLinuxLog> serviceLinuxLogList = new ArrayList<>();
        for (ServiceLinuxLogSaveDto dto : dtoList) {
            ServiceLinuxLog insertLog = new ServiceLinuxLog();
            insertLog.setUuid(UUID.uuid());
            insertLog.setSessionId(dto.getSessionId());
            insertLog.setServiceId(dto.getServiceId());
            insertLog.setExeTime(new Date());
            insertLog.setExeBy(dto.getUserId());
            insertLog.setExeCmd(dto.getExeCmd());
            serviceLinuxLogList.add(insertLog);
        }
        this.saveBatch(serviceLinuxLogList);
    }
}
